import 'package:connectivity/connectivity.dart';
import 'package:flutter/widgets.dart';

class NetNotifier with ChangeNotifier{
  //防止页面销毁后，异步任务才完成的页面数据更新报错
  bool _disposed = false;

  bool _isConnect = true;

  bool get isConnect => _isConnect;

  Future checkConnected() async {
    final ConnectivityResult connectivityResult = await Connectivity().checkConnectivity();
    debugPrint('验证联网结果：$connectivityResult');
    _isConnect = connectivityResult != ConnectivityResult.none;
    notifyListeners();
  }

  @override
  void notifyListeners() {
    if(!_disposed){
      super.notifyListeners();
    }
  }

  @override
  void dispose() {
    _disposed = true;
    super.dispose();
  }
}